package com.Guo.Filter;

import com.Guo.jwt.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Admin
 * @project JwtAuthenticationTokenFilter
 * @description
 * @date 2020-12-18-10:31
 */
@Component("oncePerRequestFilter")
class JwtAuthenticationTokenFilter extends OncePerRequestFilter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        String headertoken = request.getHeader(jwtTokenUtil.getHeader());
        System.out.println("headerToken=" + headertoken);
        System.out.println("request getMethod=" + request.getMethod());
        //如果不为空，这说明已经登录过了，无需重新登录
        if (!StringUtils.isEmpty(headertoken)) {
            //postman测试时，自动加入的前缀,要去掉
            String token = headertoken.replace("Bearer", "").trim();
            System.out.println("token=" + token);
            //判断令牌是否过期,默认是一周
            // 比较好的解决方案是:
            //登录成功获得token后,将token存储到数据库(redis)
            // 将数据库版本的token设置过期时间关15~30分钟
            //如果数据库中的token版本过期,重新刷新获取新的token
            // 注意:刷新获得新token是在token过期时间内有效。
            //如果token本身的过期(1周)，强制登录,生成新token。
            boolean check=false;
                try {
                    check = this.jwtTokenUtil.isTokenExpired(token);
                }catch (Exception e){
                    new Throwable("令牌已过期，请重新登录。"+e.getMessage());
                }
                if (!check){
                    //通过令牌获取用户名称
                    String username = jwtTokenUtil.getUsernameFromToken(token);
                    System.out.println(username);
                    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
                        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
                        if (jwtTokenUtil.validateToken(token, userDetails)) {
                            // 将用户信息存入 authentication，方便后续校验
                            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
                            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                            // 将 authentication 存入 ThreadLocal，方便后续获取用户信息
                            SecurityContextHolder.getContext().setAuthentication(authentication);
                        }
                    }
                }
            }
        chain.doFilter(request, response);
    }
}
